/**
 * 封装AJAX请求
 * 返回的是Promise对象
 */

// import axios from "axios";
import instance from "./axios";
const localStorage=window.localStorage;

function ajax(url,data={},type="GET"){
    // storage.setItem("token","abcd")
    instance.defaults.headers.common["token"] = localStorage.getItem("token");
    console.log("环境：",process.env)
    if("GET" === type){
        let paramStr="?";
        Object.keys(data).forEach(key=>{
            paramStr+=key+"="+data[key]+"&"
        });
        paramStr=paramStr.substring(0,paramStr.length-1);
        url +=paramStr
        return instance.get(url)
    }else{
        return instance.post(url,data);
    }
}

function isSuccess(response){
    return  response.data.code === "200";
}

export {ajax,isSuccess,localStorage}